package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTradeMarkMapper;
import com.atguigu.gmall.product.mapper.BaseTradeMarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTradeMarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022/10/21 14:04
 */
@Service
@SuppressWarnings("all")
public class BaseCategoryTradeMarkServiceImpl extends ServiceImpl<BaseCategoryTradeMarkMapper, BaseCategoryTrademark> implements BaseCategoryTradeMarkService {

    @Autowired
    private BaseCategoryTradeMarkMapper baseCategoryTradeMarkMapper;

    @Autowired
    private BaseTradeMarkMapper baseTradeMarkMapper;


    /**
     * 根据三级分类id查询对应的品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        // 根据分类id查询出所有的品牌id
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTradeMarkMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            // 返回品牌id集合
            List<Long> tradeMarkIds = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            return baseTradeMarkMapper.selectBatchIds(tradeMarkIds);
        }
        // 批量查询返回对应id对应的品牌属性
        return null;
    }

    /**
     * 根据三级分类id和品牌id删除对应的关联关系
     * @param category3Id
     * @param trademarkId
     */
    @Override
    public void remove(Long category3Id, Long trademarkId) {
        // 删除单条记录
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id", category3Id);
        queryWrapper.eq("trademark_id", trademarkId);
        baseCategoryTradeMarkMapper.delete(queryWrapper);
    }

    /**
     * 根据三级分类id查询当前分类底下未关联的品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        List<BaseTrademark> resultList = null;
        // 先查询到当前分类id下已经关联的id
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks = baseCategoryTradeMarkMapper.selectList(baseCategoryTrademarkQueryWrapper);
        // 查询所有的品牌的id列表 -- 判断是否包含在里面找到未关联的
        if(!CollectionUtils.isEmpty(baseCategoryTrademarks)) {
            // 整合出关联过的品牌的id列表
            List<Long> relatedList = baseCategoryTrademarks.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            // 查询出所有未关联过的列表
            resultList = baseTradeMarkMapper.selectList(null).stream().filter(baseTrademark -> {
                return !relatedList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
        }
        return resultList;
    }

    /**
     * 保存关联分类品牌
     * @param categoryTrademarkVo
     */
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        // 获取想要关联的品牌id列表
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if(!CollectionUtils.isEmpty(trademarkIdList)) {
            // 遍历进行设置值
            List<BaseCategoryTrademark> baseCategoryTrademarkList = trademarkIdList.stream().map(trademarkId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(trademarkId);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            this.saveBatch(baseCategoryTrademarkList);
        }

    }
}
